﻿namespace Digitalmes.Application.Mes;

/// <summary>
/// 存档数据
/// </summary>
[ApiExplorerSettings(GroupName = "mes")]
public sealed class ProdArchiveService : IDynamicWebApiService
{
    private readonly SugarRepository<ProdArchive> _thisRepository;
    private readonly SugarRepository<ProdArchiveData> _archiveDataRepo;

    public ProdArchiveService(SugarRepository<ProdArchive> thisRepository, SugarRepository<ProdArchiveData> archiveDataRepo)
    {
        _thisRepository = thisRepository;
        _archiveDataRepo = archiveDataRepo;
    }

    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ProdArchiveDto>> GetPagesAsync(ProdArchivePageParam param)
    {
        var model = await _thisRepository.AsQueryable()
            .LeftJoin<ProdInbound>((a, i) => a.Sn == i.Sn && a.Version == i.Version)
            .LeftJoin<ProdArchiveData>((a, i, d) => a.Id == d.ArchiveId)
            .Where(a => a.Sn == param.Sn)
            .OrderBy(a => a.Id)
            .Select((a, i, d) => new ProdArchiveDto 
            {
                Id = a.Id,
                LineId = a.LineId,
                LineCode = a.LineCode,
                OpId = a.OpId,
                OpCode = a.OpCode,
                ProcId = a.ProcId,
                ProcCode = a.ProcCode,
                Sn = a.Sn,
                ProductCode = a.ProductCode,
                Wo = a.Wo,
                PassResult = a.PassResult,
                FormualNo = a.FormualNo,
                Operator = a.Operator,
                Shift = a.Shift,
                Rfid = a.Rfid,
                CycleTime = a.CycleTime,
                ArchiveTime = a.ArchiveTime,
                Version = a.Version,
                InboundTime = i.InboundTime,
                Data = d.Data,
            })
            .ToPageAsync(param.Page, param.Limit);

        model.Items.ForEach(s =>
        {
            s.Details = new()
            {
                new ArchiveDataItemDto() { Name = "SN", Value = s.Sn, Higher = null, Lower = null, Result = "" },
            };

            if (s.Data?.Any() == true)
            {
                s.Details.AddRange(s.Data.Select(s => new ArchiveDataItemDto
                {
                    Name = s.I > 0 ? $"{s.K}-{s.I}" : s.K,
                    Value = s.V,
                    Higher = s.H,
                    Lower = s.D,
                    Result = s.R,
                }));
            }
        });

        return model;
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ProdArchiveDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<ProdArchiveDto>();
    }

    /// <summary>
    /// 获取详细过程数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<List<ArchiveDataItemDto>> GetDetailsAsync(long id)
    {
        var archiveData = await _archiveDataRepo.GetFirstAsync(s => s.ArchiveId == id);
        if (archiveData?.Data == null)
        {
            return new(0);
        }

        return archiveData.Data.Select(s => new ArchiveDataItemDto
        {
            Name = s.I > 0 ? $"{s.K}-{s.I}" : s.K,
            Value = s.V,
            Higher = s.H,
            Lower = s.D,
            Result = s.R,
        }).ToList();
    }
}
